package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import tools.DBTools;
import entity.Order;
import entity.OrderItem;
import entity.UserAddress;
import service.OrderItemService;
import service.UserAddressService;

public class OrderDetailDao {
	//根据用户ID查询订单集合
	OrderItemService oItemService = new OrderItemService(); //生成订单项业务层对象
	UserAddressService userAddressService =new UserAddressService(); //生成地址业务层
	public List<Order> getOrderById(int id)
	{
		List<Order> ordersList=new ArrayList<>();
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select * from TBL_ORDER_DETAIL where id = ?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			rs=ps.executeQuery();
			while(rs.next())
			{
				int orderId=rs.getInt(1);
				int userId=rs.getInt(2);
				UserAddress userAddressById = userAddressService.getUserAddressById(rs.getInt(3));
				String orderStatus= rs.getString(4);
				Date startTime=rs.getDate(5);
				Date endTime=rs.getDate(6);
				List<OrderItem> orderItemList = oItemService.getO_ItemById(rs.getInt("Order_id"));
				Order order=new Order(orderId, userId, userAddressById, orderStatus, startTime, endTime, orderItemList);
				ordersList.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, null);
		}
		return ordersList;
	}
	public int getOidById(int u_id)
	{
		int Oid=0;
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select order_id from tbl_order_detail where id=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, u_id);
			rs=ps.executeQuery();
			if(rs.next())
			{
				Oid=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, rs);
		}
		return Oid;
	}
	//根据订单状态和用户id获取订单
	public List<Order> getOrdersByUidStatus(int u_id,String o_status)
	{
		List<Order> orderList =new ArrayList<>();
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select * from tbl_order_detail where id=? and order_status=?";
		Order order=new Order();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, u_id);
			ps.setString(2, o_status);
			rs=ps.executeQuery();
			while(rs.next())
			{
				List<OrderItem> orderItemList = oItemService.getO_ItemById(rs.getInt("Order_id"));
				order = new Order(rs.getInt(1),
						rs.getInt(2),userAddressService.getUserAddressById(rs.getInt(3)) , rs.getString(4), 
						new java.util.Date(rs.getDate(5).getTime()),
						new java.util.Date(rs.getDate(6).getTime()), 
						orderItemList);
				orderList.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, rs);
		}
		return orderList;
	}
	//根据用户id获取全部订单
	public List<Order> getOrdersByUidAll(int u_id)
	{
		List<Order> orderList =new ArrayList<>();
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select * from tbl_order_detail where id=?";
		Order order=new Order();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, u_id);
			rs=ps.executeQuery();
			while(rs.next())
			{
				List<OrderItem> orderItemList = oItemService.getO_ItemById(rs.getInt("Order_id"));
				order = new Order(rs.getInt(1),
						rs.getInt(2),userAddressService.getUserAddressById(rs.getInt(3)) , rs.getString(4), 
						new java.util.Date(rs.getDate(5).getTime()),
						new java.util.Date(rs.getDate(6).getTime()), 
						orderItemList);
				orderList.add(order);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, rs);
		}
		return orderList;
	}
	
	//根据订单号获取对应订单详情
	public Order getOrderByOrderId(int orderId)
	{
		 Connection conn = DBTools.getInstance().getConnection();
		String sql="select * from tbl_order_detail where order_id=?";
		Order order=new Order();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, orderId);
			rs=ps.executeQuery();
			while(rs.next())				
			{
				List<OrderItem> orderItemList = oItemService.getO_ItemById(rs.getInt("Order_id"));
				order = new Order(rs.getInt(1),
						rs.getInt(2),userAddressService.getUserAddressById(rs.getInt(3)) , rs.getString(4), 
						new java.util.Date(rs.getDate(5).getTime()),
						new java.util.Date(rs.getDate(6).getTime()), 
						orderItemList);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBTools.getInstance().closeConnection(conn, ps, rs);
		}
		return order;
	}
	
	 
}
